package com.briup.web_demo2.handler;

import com.briup.web_demo2.exception.SystemException;
import com.briup.web_demo2.response.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @RestControllerAdvice == @ControllerAdvice + @ResponseBody
 * 统一异常捕获处理，响应json字符串
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SystemException.class)
    public Response systemException(SystemException exception){
        log.error("系统异常："+exception.getMessage());
        return Response.error(exception.getResponseEnum());
    }

    @ExceptionHandler(Exception.class)
    public Response exception(Exception exception){
        log.error("系统未知异常："+exception.getMessage());
        return Response.error();
    }
}